'''
主代码测试
'''
import numpy as np
import math
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
import Env.EnvMain as env


# 传参，初始化，并进行绘图准备
A = env.ACEnv(4,4)
fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')  # 需要引入包from mpl_toolkits.mplot3d import axes3d，才能使用projection='3d'的取值
dic=['我方位置',
     '敌方位置',
     '我方弹药情况',
     '我方攻击态势',
     '敌方攻击态势',
     '生存情况',
     '攻击效果我',
     '攻击效果敌']

# 初始化环境，并输出环境的主要参数
print('初始化')
a = A.reset()
# count=0
# for i in a:
#     print(dic[count])
#     print(i)
#     count+=1
# A.render(figs,ax)


# 将敌我双方拉至攻击边界
print('第一步')
b = A.FirstStep([[2,50],[1,51],[0,96],[3,108]])
# count=0
# for i in b:
#     print(dic[count])
#     print(i)
#     count+=1
A.move()    # 去除坐标列表中的初始化占位坐标
# plt.clf()       # 清除前一回合图像
# ax = figs.add_subplot(111, projection='3d')  # 重新建立三维坐标
# A.render(figs,ax)

# 进行每回合的仿真
print('第2步')
c = A.step([[[-0.1,-0.1],[-0.1,-0.1],[-0.1,-0.1],[-0.1,-0.1]],[[0,0,1,0],[1,0,0,0],[0,0,1,1],[0,0,0,1]]]) # 第二个列表不是攻击状态，而是攻击策略
# count=0
# for i in c[0]:
#     print(dic[count])
#     print(i)
#     count+=1
plt.clf()       # 清除前一回合图像
ax = fig.add_subplot(111, projection='3d')  # 重新建立三维坐标
A.render(fig,ax)




print('第3步')
c = A.step([[[-0.12,0.13],[0.01,0.15],[-0.12,-0.03],[0.02,-0.02]],[[0,0,1,0],[1,0,0,0],[0,0,1,1],[0,0,0,1]]])
# count=0
# for i in c[0]:
#     print(dic[count])
#     print(i)
#     count+=1
plt.clf()       # 清除前一回合图像
ax = fig.add_subplot(111, projection='3d')  # 重新建立三维坐标
A.render(fig,ax)

# 多次循环step()
for i in range(0,100):
    num = i + 4
    print('第 %d 步' % num)
    para = []
    angle = []
    policy = []
    rate = 0.9      # 设置随机率，用以将攻击策略转化为1的标准
    friends = A.friends     # 用于获取空中单位的存活状态和载弹量状态
    enemies = A.enemies     # 用于获取地面单位的存活状态

    '''设置机动动作矩阵'''
    for j in range(0, 4):
        Ang = np.random.uniform(-0.17, 0.17, 2)
        angle.append(Ang)
    '''设置攻击动作矩阵'''
    for K in range(0, 4):
        D = np.random.randint(0, 1, 4)  # 生成初始攻击策略矩阵
        R = np.random.uniform(0, 1)     # 设置随机率
        S = np.random.randint(0, 4, 2)  # 生成攻击矩阵可能变为1的点，S[0]、S[1]
        s1 = S[0]
        s2 = S[1]
        '''判断空中单位的损伤状态，载弹量和地面单位的损伤状态对攻击策略的影响'''
        if friends[K].live == 1 and friends[K].NumberOfMissile == 2:
            if enemies[s1].live == 1 and R <= rate:
                D[s1] = 1
            if enemies[s2].live == 1 and R <= rate:
                D[s2] = 1
        elif friends[K].live == 1 and friends[K].NumberOfMissile == 1:
            if enemies[s1].live == 1 and R <= rate:
                D[s1] = 1
        policy.append(D)
    para.append(angle)
    para.append(policy)

    c = A.step(para)
    # count = 0
    # for i in c[0]:
    #     print(dic[count])
    #     print(i)
    #     count += 1
    plt.clf()  # 清除前一回合图像
    ax = fig.add_subplot(111, projection='3d')  # 重新建立三维坐标
    A.render(fig, ax)


A.excel_save()

""" # excel模块测试
import Env.Tools.ExcelFunc as xl
exc=xl.excel()
exc.out_to_excel([111,'2asd'])
exc.save()
"""
